
namespace Behemoth.SharePoint {

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;

    using Microsoft.SharePoint;

  public class SPFieldTools {

    private SPFieldTools() { }

    #region TryGetField and Supporting Methods

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryGetField(SPFieldCollection fields, string internalNameOrTitle) {
        return TryGetField(fields, internalNameOrTitle, true);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryGetField(SPFieldCollection fields, string internalNameOrTitle, bool throwException) {
        SPField field = null;
        if (throwException) {
            field = fields.GetFieldEx(internalNameOrTitle);
        } else {
            bool success = fields.TryGetField(internalNameOrTitle, out field);
            if (!success)
                return null;
        }
        return field;
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryGetField(SPFieldCollection fields, Guid fieldId) {
        return TryGetField(fields, fieldId, true);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryGetField(SPFieldCollection fields, Guid fieldId, bool throwException) {
        SPField field = null;
        if (throwException) {
            field = fields.GetFieldEx(fieldId);
        } else {
            bool success = fields.TryGetField(fieldId, out field);
            if (!success)
                return null;
        }
        return field;
    }

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryAndGetField(SPFieldCollection fields, string internalNameOrTitle) {
        return TryGetField(fields, internalNameOrTitle, true);
    }

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.TryGetField or SPFieldExtensions' SPFieldCollection.GetFieldEx instead.")]
    public static SPField TryAndGetField(SPFieldCollection fields, string internalNameOrTitle, bool throwException) {
        return TryGetField(fields, internalNameOrTitle, throwException);
    }

#endregion

    #region Reflection Based Field Proeprty Access

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.")]
    public static bool? SetFieldProperty(SPField field, string name, bool? value) {
        return field.SetFieldProperty(name, value);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.")]
    public static string SetFieldProperty(SPField field, string name, string value) {
        return field.SetFieldProperty(name, value);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.")]
    public static bool SetFieldProperty(SPField field, string name, bool value) {
        return field.SetFieldProperty(name, value);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPField.SetFieldProperty instead.")]
    public static int SetFieldProperty(SPField field, string name, int value) {
        return field.SetFieldProperty(name, value);
    }

    #endregion

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.FieldExists instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.FieldExists instead.")]
    public static bool FieldExists(SPFieldCollection fields, string internalNameOrTitle) {
        return fields.FieldExists(internalNameOrTitle);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.FieldExists instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.FieldExists instead.")]
    public static bool FieldExists(SPFieldCollection fields, Guid id) {
        return fields.FieldExists(id);
    }

    /*
    /// <summary>
    /// Attempts to get a list item fieldName value by its internal name OR Title.
    /// </summary>
    /// <param name="listItem"></param>
    /// <param name="name"></param>
    /// <param name="throwException"></param>
    /// <returns></returns>
    public static object GetListItemValue(SPListItem listItem, string name, bool throwException) {
      SPField fieldName = TryGetField(listItem.Fields, name, throwException);
      if (fieldName == null)
        return null;
      return listItem[fieldName.Title];
    }
    public static object GetListItemValue(SPListItem listItem, string name) {
      return GetListItemValue(listItem, name, true);
    }

      /// <summary>
      /// Tries to get a folder, returns null if unsuccessful.
      /// Calls SPWeb.GetFolder and then SPFolder.Exists. Checks for
      /// known quirky behavior in the GetFolder method and SPFolder object.
      /// </summary>
      /// <param name="web">web site to get folder from</param>
      /// <param name="folderPath">string path to pass into GetFolder</param>
      /// <returns></returns>
    public static SPFolder SPSafeGetFolder(SPWeb web, string folderPath) {
        SPFolder folder = web.GetFolder(folderPath);
        if (folder == null || folder.GetType() != typeof(SPFolder))
            return null;
        try {
            if (!folder.Exists)
                return null;
        } catch (ArgumentException) {
            return null;
        }
        return folder;
    }
    */

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldId instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPFieldCollection.GetFieldId instead.")]
    public static Guid GetFieldId(SPFieldCollection fields, string internalNameOrTitle) {
        return fields.GetFieldId(internalNameOrTitle);
    }

    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPWeb.MakeFieldsList instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPWeb.MakeFieldsList instead.")]
    public static List<SPField> MakeFieldsList(SPWeb web, List<string> fields) {
        return web.MakeFieldsList(fields);
    }
    /// <summary>
    /// This method is deprecated. Please use SPFieldExtensions' SPWeb.MakeFieldsList instead.
    /// </summary>
    [Obsolete("This method is deprecated. Please use SPFieldExtensions' SPWeb.MakeFieldsList instead.")]
    public static List<SPField> MakeFieldsList(SPWeb web, List<Guid> fields) {
        return web.MakeFieldsList(fields);
    }

  } // class

} // namespace
